<?php
/**
 * @file jiemengtaglistcontroller.php
 * @author LinLin
 * @date 2016年4月19日 下午3:01:50
 * @brief 
 */
namespace haibao\cms\business\control\pc\channel\jiemeng;

use haibao\frame\data\query\Condition;

class JieMengTagListController extends \haibao\cms\business\control\JiemengController{

    public function __construct($updateTime = null,$controlId,$isPublish){
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $currentPage = $this->getParameter(self::$CURRENTPAGE);
        $pageable = $this->getParameter(self::$PAGEABLE);
        $siteTemplateId = $this->getParameter(self::$SITE_TEMPLATE_ID);
        $this->replaceAdPosition($controlData['auto']);
    
        $tagInfo = $this->getParameter(self::$TAG_INFO);
        $this->conditionTagId = $tagInfo->Id;
        $this->topTagId = $tagInfo->TopId;
    
        $topTagList = self::getTopTags();
        $topTagName = $topTagList[$this->topTagId]['name'];
        $topTagUrl = $topTagList[$this->topTagId]['tagchannelurl'];
        $logo = $topTagList[$this->topTagId]['logo'];
        if($controlData['manual']){
            $dataCondition = $controlData['manual'];
            $dataCondition['currentPage'] = $currentPage;
    
            	
            if(!$this->isPublish){
                $this->assign('viewName','priview');
                $this->assign('pageUrl','/site/preview.html');
            }else{
                $this->packageUrl();
            }
            
            $this->getArticleNewList($dataCondition);
            	
            $this->assign('siteTemplateId', $siteTemplateId);
            $matchDesc = $tagInfo->Desc;
            	
            if(!preg_match('/\<p\>([^\s+].*?)\<\/p\>/',$tagInfo->Desc,$matchs)){
                if(!empty($matchs)){
                    $matchDesc = '';
                }
            }
            $this->assign('tagName', $tagInfo->TagName);
            $this->assign('desc', $tagInfo->Desc);
            $this->assign('matchDesc', $matchDesc);
    
            $this->assign('topTagName', $topTagName);
            $this->assign('topTagUrl', $topTagUrl);
            $this->assign('logo', $logo);
            	
            $path = $this->generateHtmlPath(__CLASS__,$this->conditionTagId.$currentPage);
            $this->generateHtml($path,$content);
            $content = $this->getHtml($path);
            $content = $this->replaceCDNLink($content);
            	
            return $content;
        }
    }
    
    /**
     * 封装发布的地址url
     */
    private function packageUrl(){
        $topIds = self::getTopTags();
        $url = $topIds[$this->topTagId]['channelurl'].'/diy/'.$this->conditionTagId.'/';
        $this->assign('pageUrl',$url);
        $this->assign('pageSize',16);
        $this->assign('viewName','publish');
    }
    
    /**
     * 获取最新资讯列表
     * @param : Array $queryCondition 查询条件
     */
    private function getArticleNewList($dataCondition){
        $dataCondition['source'] = __CLASS__.$this->conditionTagId;
		$dataCondition['level'] = 1;
		$dataCondition['isProImage'] = true;
		$dataCondition['isTag'] = true;
		$dataCondition['isReturnTag'] = true;
		$dataCondition['isParentTag'] = false;
		$dataCondition['isImageColl'] = false;
		$dataCondition['isReturnPrimary'] = false;
		$proLevel = \haibao\cms\model\data\ArticleProImage::Level_FIVE;
		$dataCondition['proImageLevel'] = array($proLevel);
		list($articles,$proImage,$tagInfo,$articleTag,$pager) = $this->getArticleControl(5,$dataCondition);
		//print_r($articleTag);
		$finalArr = $articleIds = array();
		foreach($articles as $key=>$article){
			$temp = array();
			$articleIds[] = $article->Id;
			$temp['Id'] = $article->Id;
			$temp['Title'] = $article->Title;
			$temp['SiteType'] = $article->SiteType;
			$temp['Channel'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
			$temp['Date'] = $this->getformatDate($article->PublishTime->format('Y-m-d'));
			$temp['EnMonth'] = strtoupper($article->PublishTime->format('M'));
			
			if(isset($articleTag[$article->Id])){
				foreach($articleTag[$article->Id] as $m=>$primary){
					if($primary['IsPrimaiy'] == 1){
					$temp['TagName'] = $tagInfo[$primary['TagId']]['TagName'];
					$temp['TagUrl'] = $tagInfo[$primary['TagId']]['Url'];
					}
				}
			}else{
				$temp['TagUrl'] = $temp['TagName'] = '';
			}
			array_push($finalArr,$temp);
		}

		$this->assign('pageCount',$pager->getPageCount());
		$this->assign('pager', $pager);
		$this->assign('tagList',$finalArr);
    }
    
    private function getformatDate($date) {
        $finalDate = '';
        $dateTime = strtotime ( $date );
        $today = strtotime(date('Y-m-d'));
        $nowMonth = date ( 'Ym' );
        $month = date ( 'Ym', $dateTime );
        $diff = $today - $dateTime;
        if ($diff == 0 && $month == $nowMonth) {
            $finalDate = '今天';
        } elseif ($diff == 3600*24 && $month == $nowMonth) {
            $finalDate = '昨天';
        } else {
            $finalDate = date ( 'Y-m-d', $dateTime );
        }
        return $finalDate;
    }
}